package jonggyuni.service;

import jonggyuni.interfaces.CDInfoService;
import jonggyuni.model.CDInfoVO;
import java.sql.*;

public class JDBC_CDInfo implements CDInfoService{
	
	//JDBC 접속정보
	private static final String DRV = "oracle.jdbc.OracleDriver";
	private static final String URL = "jdbc:oracle:thin:@//localhost:1521/xe";
	private static final String USR = "scott";
	private static final String PWD = "TIGER";
	
	//SQL문 작성
	
	private static final String insertSQL = "insert into cdinfo(cd_id, title, publisher, artist, price)"+
			"VALUES (?,?,?,?,?)";
	private static final String selectSQL = "select * from cdinfo "+" order by cd_id desc";

	private static final String updateSQL = " update cdinfo set cd_id, title, publisher, artist, price";
	
	private static final String deleteSQL = " delete from cdinfo where cd_id = ? ";

	@Override
	public boolean newCDInfo(CDInfoVO cd) {
		Connection conn = null;
		PreparedStatement ps = null;
		boolean result = false;
		
		try{
			Class.forName(DRV);
			conn = DriverManager.getConnection(URL, USR, PWD);
			ps = conn.prepareStatement(insertSQL);
			
			ps.setString(1, cd.getCd_id());
			ps.setString(2, cd.getTitle());
			ps.setString(3, cd.getPublisher());
			ps.setString(4, cd.getArtist());
			ps.setInt(5, cd.getPrice());
			
			int cnt = ps.executeUpdate();
			
			if(cnt > 0) result = true;			
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			if(ps!=null)
				try{ps.close();} catch(Exception e){}
			if(conn!=null)
				try{conn.close();} catch(Exception e){}
		}	
		
		return false;
	}

	@Override
	public String readCDInfo() {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String result = "";
		
		try{
			Class.forName(DRV);
			conn = DriverManager.getConnection(URL, USR, PWD);
			ps = conn.prepareStatement(selectSQL);
			rs = ps.executeQuery();
			
			while(rs.next()){
				for(int i=1; i<=30; ++i){
					result += rs.getString(i);
				}
				result += "\n";
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			if(rs!=null)
				try{rs.close();} catch(Exception e){}
			if(ps!=null)
				try{ps.close();} catch(Exception e){}
			if(conn!=null)
				try{conn.close();} catch(Exception e){}
		}
		return result;
	}

	@Override
	public boolean modifyCDInfo(CDInfoVO sj) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean removeCDInfo(String key) {
		// TODO Auto-generated method stub
		return false;
	}

	

}
	